/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006-2008 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import who.tmap.common.Constants;
import who.tmap.toolkit.impl.MappingToolKitImpl;
import who.tmap.ui.JFrameMultiLayerThematicMap;
import who.tmap.ui.util.ArgumentsHelper;
import who.tmap.ui.util.MiscUIHelper;

/**
 * This the main class to launch TMap Designer
 * @author stl
 *
 */
public class TMapDesigner {

    private static Logger logger = Logger.getLogger(TMapDesigner.class);
    
    private static String[] args;
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
/*    private static void createAndShowGUI() {
    	
		ArgumentsHelper argsHelper =ArgumentsHelper.getInstance();
		argsHelper.setArgs( args );	    
	    
	    if ( argsHelper.askForHelp() ) {
	    	argsHelper.outputHelpMessage();
	    } else {
	    	if ( argsHelper.contains( ArgumentsHelper.DEV_ENV ) || args.length == 0 ) {
	    		BasicConfigurator.configure(); 
	    	} else {
	    		PropertyConfigurator.configure( TMapDesigner.class.getResource( Constants.LOG4J_COMMAND_LINE_PROPERTIES_FILE ) );
	    	}
	    	
		    logger.info( "TMap Designer V" + MappingToolKitImpl.VERSION );
		    String buildDate =new MiscUIHelper().getBuildDate();
		    if ( !"".equals( buildDate ) ) { 
		    	logger.info( "Build date " + buildDate );
		    }
		    
	    	// Use OS look & feel
		    // Seems to cause trouble with Windows Look & Feel
		    // For instance, the CD combo box cannot be opened (sometimes only) 
	    	try {
	    	    UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
	    	} catch ( Exception e ) {
	    	    logger.error( e );
	    	}
	    	
	    	JFrameMain jFrameMain =new JFrameMain();
	    	
	    	if ( !argsHelper.contains( ArgumentsHelper.QUIET ) ) {
	    		jFrameMain.setVisible( true );
	    	}
	    }
    }*/
    
    private static void createAndShowNewGUI() {
		ArgumentsHelper argsHelper =ArgumentsHelper.getInstance();
		argsHelper.setArgs( args );	    
	    
	    if ( argsHelper.askForHelp() ) {
	    	argsHelper.outputHelpMessage();
	    } else {
	    	if ( argsHelper.contains( ArgumentsHelper.DEV_ENV ) || args.length == 0 ) {
	    		BasicConfigurator.configure(); 
	    	} else {
	    		PropertyConfigurator.configure( TMapDesigner.class.getResource( Constants.LOG4J_COMMAND_LINE_PROPERTIES_FILE ) );
	    	}
	    	
		    logger.info( "TMap Designer V" + MappingToolKitImpl.VERSION );
		    String buildDate =new MiscUIHelper().getBuildDate();
		    if ( !"".equals( buildDate ) ) { 
		    	logger.info( "Build date " + buildDate );
		    }
		    
	    	// Use OS look & feel
		    // Seems to cause trouble with Windows Look & Feel
		    // For instance, the CD combo box cannot be opened (sometimes only) 
	    	try {
	    	    UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
	    	} catch ( Exception e ) {
	    	    logger.error( e );
	    	}
	    	
	    	JFrameMultiLayerThematicMap jFrame =new JFrameMultiLayerThematicMap();
	    	
	    	if ( !argsHelper.contains( ArgumentsHelper.QUIET ) ) {
	    		jFrame.setVisible( true );
	    	}
	    }
    }
    
	public static void main( String[] args ) throws Exception {
		TMapDesigner.args =args;
		
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowNewGUI();
            }
        });
	}
}
